# Client program
import os
import thread
import sys
from socket import *



host = "localhost"
port = 23000
buf = 1024
		
if len(sys.argv)>1:
	# First parameter is host
	host = sys.argv[1]
if len(sys.argv)>2:
	port = sys.argv[2]
# Set the socket parameters
print "Sending to Server at "+str(host)+":"+str(port)
addr = (str(host),int(port))

# Create socket


def listenToServer(host,port):
	#thehost = "localhost"
	#theport = 23001
	buf = 1024
	#addr = (str(host),int(port))
	addr = ('',int(port))
	UDPSock = socket(AF_INET,SOCK_DGRAM)
	UDPSock.bind(addr)
	while(1):
		data,addr = UDPSock.recvfrom(buf)
		print data


serverport = int(port)+1
print "Listening to Server on localhost:"+str(serverport)
listenToServerThread = thread.start_new_thread(listenToServer,(host,serverport))
# Send messages
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.sendto("",addr)
while (1):
	data = raw_input('> ')
	if data.upper()=="EXIT":
		os._exit(0)
	else:
		
		#print "sending to "+str(addr)
		UDPSock.sendto(data,addr)

# Close socket
UDPSock.close()
